<template>
  <div>
    <Modal v-model="couponPop" width="760" class-name="vertical-center-modal" :closable="false">
      <div slot="header" class="head">
        <div class="pop-head">
          <div class="pop-tit">{{ $t('personal.select_title') }}</div>
          <Icon type="md-close" size="26" color="#242424" class="close" @click="cancelBtn()" />
        </div>
      </div>
      <t-loading v-if="loading"></t-loading>
      <Row :gutter="32" class="box" v-if="couponDataList.length > 0">
        <Col span="12" v-for="(item, index) in couponDataList" :key="index">
        <div class="coupons" :class="getClass(item)" @click="onCouponSelect(item, index)">
          <div class="cover-up" v-if="item.status != 'UNUSED' || item.isAvailable == false"></div>
          <div class="pad-sty">
            <div class="coupon-num">
              <div class="amount-info">
                <span class="amount" v-if="item.type != 'ZKQ'">
                  {{ userInfo.currencySymbol }}
                </span>
                <span class="amount">
                  {{ item.type == 'ZKQ' ? `${bigTimes((item.discountShowOFF || 0), 10)}%` : item.discountShowOFF || 0 }}
                </span>
                <span class="text">OFF</span>
              </div>
              <!-- 选中 -->
              <img src="@/assets/images/parcel/select_icon.png" v-if="active === index" class="select-icon" alt="">
              <!-- 未选中 -->
              <img src="@/assets/images/parcel/un_select_icon.png" v-else class="select-icon" alt="">
            </div>
            <div class="condition" :class="textClass(item)">
              <span class="text" v-if="item.minPrice > 0">
                {{ `${$t('personal.MinFreight')} ${userInfo.currencySymbol}${item.minPrice || 0}
                ${userLanguage.code == 'zh_CN' ? $t('personal.required') : ''}` }}
              </span>
              <span class="text" v-else>{{ $t("personal.unlimited_text") }}</span>
            </div>
            <div class="date">
              <span>
                {{ item.startTime || '' }}~{{ item.endTime || '' }}
              </span>
            </div>
          </div>
          <div class="un-reason" v-if="item.status != 'UNUSED' || item.isAvailable == false">
            <div class="text">{{ item.noAvailableReason || '——' }}</div>
          </div>
          <div class="tip" v-else>{{ item.description || '——' }}</div>
        </div>
        </Col>
      </Row>
      <div v-else class="no-data">
        <img src="@/assets/images/empty/empty_prize.png" class="empty-img" alt="">
      </div>
      <div slot="footer" class="footer-sty">
        <div></div>
        <div class="handle-sty">
          <div class="handle-btn" @click="cancelBtn()">{{ $t('pay.cancel') }}</div>
          <div class="handle-btn confirm-btn" @click="confirmBtn()">{{ $t('personal.Search-confirm') }}</div>
        </div>
      </div>
    </Modal>
  </div>
</template>

<script>
import TLoading from "@/components/t-loading";
import { bigTimes } from "@/common/digital-computation";
import dayjs from 'dayjs';
export default {
  components: { TLoading },
  props: ['couponDataList', 'couponType', 'userCurrency', 'selectInfo', 'userInfo', 'userLanguage'],
  data() {
    return {
      couponPop: false,
      loading: false,
      active: '',
      infoVal: {}
    }
  },
  methods: {
    dayjs,
    bigTimes,
    getClass(item) {
      if (item.targetObject === 'EXPRESS') {
        return 'express-item';
      } else if (item.targetObject === 'ORDER') {
        return 'order-item';
      } else {
        return ''
      }
    },
    textClass(item) {
      if (item.targetObject === 'EXPRESS') {
        return 'express-text';
      } else if (item.targetObject === 'ORDER') {
        return 'order-text';
      } else {
        return ''
      }
    },
    open(subItem) {
      if (this.couponType === 'ORDER') {
        this.infoVal = subItem;
      }
      this.couponPop = true;
    },
    onCouponSelect(item, index) {
      if (!item.isAvailable || item.status != 'UNUSED') return;
      if (this.active === index) {
        this.active = '';
      } else {
        this.active = index;
        this.selObj = item;
      }
    },
    cancelBtn() {
      this.couponPop = false;
      this.active = '';
    },
    confirmBtn() {
      if (this.active === '') {
        if (this.couponType === 'ORDER') {
          // 订单反选取消
          this.$emit('uncheck', this.infoVal.id, this.selObj);
          this.selObj = {};
        } else if (this.couponType === 'EXPRESS') {
          // 运单反选取消
          this.selObj = {};
          this.$emit('amountAssignment', { couponCustomerId: '' });
          this.$emit('uncheck');
        }
        this.couponPop = false;
      } else {
        if (this.couponType === 'ORDER') {
          if (this.selObj) {
            this.$emit("handleInfo", this.infoVal.id, this.selObj);
            this.couponPop = false;
          } else {
            this.couponPop = false;
          }
        } else if (this.couponType === 'EXPRESS') {
          if (this.selObj.id) {
            this.$emit('loadingFc', true);
            this.$emit("handleInfo", this.selObj);
          } else {
            this.$emit('uncheck')
          }
          this.couponPop = false;
        }
      }
    }
  }
}
</script>

<style scoped lang="less">
:deep(.ivu-modal-content) {
  border-radius: 12px;

  .ivu-modal-header {
    border: none;
    padding: 0;
  }

  .ivu-modal-body {
    padding: 0 42px;
  }

  .ivu-modal-footer {
    padding: 42px;
    border: none;
  }
}

.vertical-center-modal {
  display: flex;
  align-items: center;
  justify-content: center;

  .head {
    border-radius: 12px 12px 0 0;
    background: linear-gradient(179deg, #ffdeca66 0%, #FFFFFF 100%);

    .pop-head {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin: 0 42px;
      padding: 32px 0;

      .pop-tit {
        font-family: PingFang SC, PingFang SC;
        font-weight: 500;
        font-size: 24px;
        color: #1E1714;
        line-height: 28px;
      }

      .close {
        cursor: pointer;
      }
    }
  }

  .box {
    overflow: auto;
    height: 460px;

    .coupons {
      min-height: 186px;
      padding-bottom: 12px;
      cursor: pointer;
      margin-bottom: 32px;
      position: relative;

      .cover-up {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 3;
        background: #FFFFFF;
        opacity: 0.5;
        filter: grayscale(100%);
        cursor: no-drop !important;
      }

      .pad-sty {
        padding: 14px 24px;

        .coupon-num {
          display: flex;
          justify-content: space-between;
          align-items: center;

          .amount-info {
            font-family: HarmonyOS Sans SC;
            font-weight: bold;
            color: #FFFFFF;

            .amount {
              font-size: 32px;
              line-height: 32px;
            }

            .text {
              font-size: 24px;
              line-height: 24px;
              margin-left: 4px;
            }
          }

          .select-icon {
            width: 20px;
            height: 20px;
          }
        }

        .condition {
          min-width: 100px;
          padding: 2px 10px;
          background: #FFFFFF;
          font-size: 16px;
          border-radius: 4px;
          text-align: center;
          display: inline-block;
          margin-top: 10px;
        }

        .express-text {
          font-weight: 500;
          font-size: 14px;
          color: #FF6A25;
          line-height: 19px;
        }

        .order-text {
          font-weight: 500;
          font-size: 14px;
          color: #FF1140;
          line-height: 19px;
        }

        .date {
          color: #ffffffb3;
          font-size: 16px;
          margin-top: 10px;
        }
      }

      .tip {
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        font-size: 14px;
        color: #ffffffb3;
        line-height: 16px;
        padding-left: 24px;
        padding-right: 24px;
      }

      .un-reason {
        background: #F7F9FC;
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;

        .text {
          font-weight: 500;
          font-size: 16px;
          color: #767B93;
          line-height: 16px;
          margin: 12px;
        }
      }
    }

    .express-item {
      background: url(@/assets/images/home/express_bg.png) no-repeat;
      background-size: 100% 100%;
    }

    .order-item {
      background: url(@/assets/images/home/order_bg.png) no-repeat;
      background-size: 100% 100%;
    }

    .disable {
      filter: grayscale(100%);
      cursor: no-drop !important;
    }
  }

  .box::-webkit-scrollbar {
    display: none;
  }

  .no-data {
    display: flex;
    justify-content: center;

    .empty-img {
      width: 200px;
      height: 200px;
    }
  }

  .footer-sty {
    display: flex;
    justify-content: space-between;

    .handle-sty {
      display: flex;
      align-items: center;
      gap: 24px;

      .handle-btn {
        min-width: 112px;
        height: 44px;
        border-radius: 8px 8px 8px 8px;
        border: 1px solid #E5E5E5;
        font-weight: 400;
        font-size: 16px;
        color: #1E1714;
        line-height: 44px;
        text-align: center;
        cursor: pointer;
      }

      .confirm-btn {
        background-color: #1E1714;
        color: #FFFFFF;
        border: none;
      }
    }
  }
}
</style>